package util;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.image.BufferedImage;

public class NeonConsoleLog extends JFrame {
    JPanel searchImagePanel = new JPanel();
    JCheckBox checkBoxAutoE = new JCheckBox("<html><p style='color:#000000'>自动连杆</p><font color=#aaaaaa>钓上一条鱼之后，自动按E继续钓鱼</font>");

    public NeonConsoleLog() {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(e -> {
            System.out.println("--------------------------" + e);
            return false;
        });
        // setUndecorated(true);
        // this.setOpacity(0.5f);
        this.setTitle("霓虹深渊-自动钓鱼");
        checkBoxAutoE.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        checkBoxAutoE.setAlignmentX(Component.LEFT_ALIGNMENT);
        checkBoxAutoE.setBackground(Color.WHITE);
        this.getContentPane().add(checkBoxAutoE);

        JLabel label = new JLabel("这是一个label");
        label.setBackground(Color.BLUE);
        label.setBorder(new LineBorder(Color.BLACK));
        label.setBorder(new EmptyBorder(10, 10, 10, 10));
//        label.setAlignmentY(1.0f);
        this.getContentPane().add(label);
//        this.add(button);

        this.getContentPane().setBackground(Color.WHITE);
        this.setBounds(760, 250, 400, 600);
        this.setVisible(true);
        // 窗口置顶
        this.setAlwaysOnTop(true);
        // 关闭窗口并退出程序
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        // 设置盒子布局
        // 参考：https://www.likecs.com/show-170200.html#sc=2900
        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        this.getContentPane().add(searchImagePanel);

        for (Component component : this.getContentPane().getComponents()) {
            System.out.println(component);
        }

    }

    BufferedImage image;
    String[] msg = new String[]{"", "", ""};
    static String s1 = "开始钓鱼了...";
    String s2 = "中鱼！起√！";
    String s3 = "钓到鱼了！";

    public void pushMsg(String pushMsg) {
        msg[0] = msg[1];
        msg[1] = msg[2];
        msg[2] = pushMsg;
        drawText();
    }

    public void updateMsg(String updateMsg) {
        msg[2] = updateMsg;
        drawText();
    }

    public void pushImage(BufferedImage image) {
        this.image = image;
        drawText();
    }

    public void drawText() {
//        Container contentPane = this.getContentPane();
//        Graphics graphics = contentPane.getGraphics();
        Graphics graphics = searchImagePanel.getGraphics();
        graphics.clearRect(0, 0, searchImagePanel.getWidth(), searchImagePanel.getHeight());
        graphics.setColor(Color.RED);
        graphics.fillRect(0, 0, getWidth(), getHeight());
        Font font = new Font("微软雅黑", Font.PLAIN, 24);
        graphics.setFont(font);
        graphics.setColor(Color.BLACK);
        graphics.drawString(msg[0], 10, 34);
        graphics.drawString(msg[1], 10, 68);
        graphics.drawString(msg[2], 10, 102);
        if (this.image != null) {
            graphics.drawImage(image, 10, 126, 300, 300, this);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        NeonConsoleLog nenoConsoleLog = new NeonConsoleLog();
        int i = 0;
        while (true) {
            nenoConsoleLog.pushMsg(s1 + ++i);
            Thread.sleep(100);
        }
    }
}